package com.tiankun.eurekauserinfo.controller;

import com.tiankun.eurekauserinfo.exception.BusinessException;
import com.tiankun.eurekauserinfo.pojo.User;
import com.tiankun.eurekauserinfo.service.UserService;
import com.tiankun.eurekauserinfo.utils.JsonResult;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * User: bear
 * Date: 2020/11/11
 * Description
 * Version: V1.0
 */
@RestController
@RequestMapping("user")
@CrossOrigin   //用于实现跨域的注解
public class UserController {

    @Resource
    private UserService userService;


    @PostMapping(value = "findByPhone", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findByPhone(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        User user = userService.findByPhone(String.valueOf(map.get("userPhone")));

        jsonResult.setCode("200");
        jsonResult.setData(user);
        jsonResult.setMsg("user个人信息");
        return jsonResult.getValues();
    }

    @PostMapping(value = "update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map update(@RequestBody User user) {
        System.out.println(user);
        JsonResult jsonResult = new JsonResult();
        Integer integer = userService.updateUser(user);
        if (integer != 0) {
            jsonResult.setMsg("修改成功");
            jsonResult.setCode("200");
        } else {
            jsonResult.setMsg("修改失败");
            jsonResult.setCode("500");
        }
        return jsonResult.getValues();
    }

    @PostMapping(value = "updatePassword", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map updatePassword(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = userService.updatePassword((Integer) map.get("userId"), (String) map.get("password"), (String) map.get("newPassword"));
        if (integer == 1) {
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
            return jsonResult.getValues();
        } else {
            throw new BusinessException("出现未知异常");
        }
    }

    @PostMapping(value = "findPassword", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Map findPassword(@RequestBody Map<String, Object> map) {
        JsonResult jsonResult = new JsonResult();
        Integer integer = userService.findPassword((String) map.get("phone"), (String) map.get("yzm"), (String) map.get("newPassword"));
        if (integer != 1) {
            throw new BusinessException("用户不存在");
        }
        jsonResult.setCode("200");
        jsonResult.setMsg("密码找回成功");
        return jsonResult.getValues();
    }
//
//    @PostMapping(value = "updateImg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public Map updateImg(@RequestBody Map<String, Object> map) {
//        User user = new User();
//        user.setId((Integer) map.get("userId"));
//        user.setImg((String) map.get("img"));
//        //修改user信息，修改成功会返回1
//        Integer integer = userService.updateUser(user);
//        if (integer == 1) {
//            jsonResult.setCode("200");
//            jsonResult.setMsg("修改头像成功");
//        }
//        return jsonResult.getValues();
//    }


}
